import {
  Entity,
  PrimaryGeneratedColumn,
  Column,
  ManyToOne,
  JoinColumn,
} from "typeorm";
import { ManualTimestamp } from "../../../decorators/manual-timestamp.decorator";
import { CasinoChibiRound } from "./casino-chibi-round.entity";

export enum ChibiSide {
  WEI = 1, // 魏国
  SHU = 2, // 蜀国
  WU = 3, // 吴国
}

export enum BetStatus {
  BETTING = 0, // 下注中
  DRAWN = 1, // 已开奖
  SETTLED = 2, // 已结算
}

@Entity("casino_chibi_bets")
@ManualTimestamp()
export class CasinoChibiBet {
  @PrimaryGeneratedColumn()
  id!: number;

  @Column({ comment: "期数ID" })
  roundId!: number;

  @Column({ comment: "用户ID" })
  userId!: number;

  @Column({ comment: "角色ID" })
  characterId!: number;

  @Column({ type: "tinyint", comment: "支援国家：1-魏，2-蜀，3-吴" })
  side!: ChibiSide;

  @Column({ type: "bigint", default: 0, comment: "金币下注" })
  betGold!: number;

  @Column({ type: "bigint", default: 0, comment: "银币下注" })
  betSilver!: number;

  @Column({ type: "tinyint", default: BetStatus.BETTING, comment: "状态" })
  status!: BetStatus;

  @Column({ type: "bigint", default: 0, comment: "中奖金币" })
  winGold!: number;

  @Column({ type: "bigint", default: 0, comment: "中奖银币" })
  winSilver!: number;

  @Column("datetime", { name: "created_at", comment: "创建时间" })
  created_at!: Date;

  @Column("datetime", {
    name: "updated_at",
    nullable: true,
    comment: "更新时间",
  })
  updated_at!: Date | null;

  // 关联关系 - 移除外键约束，只保留字段引用
  // // @ManyToOne => CasinoChibiRound, (round) => round.chibiBets)
  // // @JoinColumn
  // // round!: CasinoChibiRound;

  // 计算属性
  get isWinning(): boolean {
    return this.winGold > 0 || this.winSilver > 0;
  }

  get isLosing(): boolean {
    return (
      this.status === BetStatus.SETTLED &&
      this.winGold === 0 &&
      this.winSilver === 0
    );
  }

  get totalBetAmount(): number {
    return this.betGold + this.betSilver;
  }

  get totalWinAmount(): number {
    return this.winGold + this.winSilver;
  }

  get profit(): number {
    return this.totalWinAmount - this.totalBetAmount;
  }

  get sideName(): string {
    const names = {
      [ChibiSide.WEI]: "魏国",
      [ChibiSide.SHU]: "蜀国",
      [ChibiSide.WU]: "吴国",
    };
    return names[this.side] || "未知";
  }

  get statusName(): string {
    const names = {
      [BetStatus.BETTING]: "下注中",
      [BetStatus.DRAWN]: "已开奖",
      [BetStatus.SETTLED]: "已结算",
    };
    return names[this.status] || "未知";
  }

  get hasGoldBet(): boolean {
    return this.betGold > 0;
  }

  get hasSilverBet(): boolean {
    return this.betSilver > 0;
  }
}
